package org.phalanx.servlet.webmessages;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.phalanx.messenging.TestMessage;
import org.phalanx.server.PhalanxServer;
import org.phalanx.server.ServerCache.CacheManager;
import org.phalanx.server.user.PhalanxUser;
import org.phalanx.servlet.websession.UserWebSessionManager;

public class TestContext implements WebMessageable {
	private PhalanxServer pServer;
	public static final String TEST_CONTEXT = "test";
	
	public TestContext(PhalanxServer pServer){
		this.pServer = pServer;
	}

	@Override
	public void onContextLoadable(String context, Cookie authCookie,
			HttpServletRequest req, HttpServletResponse resp) {
		if (context.equals(TEST_CONTEXT)){
			resp.setContentType("text/plain");
			String aValue = req.getParameter("a");

			if (aValue != null){
				TestMessage t = new TestMessage();
				t.setValueA(aValue);
				CacheManager cacheMgr = pServer.getCacheManager();

				PhalanxUser u = (PhalanxUser)cacheMgr.getElement("TokenSessionCache", authCookie.getValue());
				UserWebSessionManager sessionMgr = new UserWebSessionManager(cacheMgr);

				sessionMgr.sendMessage(u.getId(), t);
			}
		}
	}

}
